<?php		
/**
 * 注：此模板为自动生成模板，需要根据实际情况调整修改
 * 此模板对 无主键/单一主键 表的支持较好，联合主键表在下一版本开发
 */					
class Step_count {
			
		    //数组	
			private $array;
			
					
			public function Step_count($array){
									  
						$this->array = $array;
				
						 self::setDefault();
				
		    }
		   
		   	
			private function setDefault(){
				if(count($this->array)==0){

					self::setId("");
					self::setOpenid("");
					self::setImg("");
					self::setName("");
					self::setMarkName("");
					self::setState("");
					self::setPositionDay("");
					self::setPositionAll("");
					self::setStepyestoday("");
					self::setStepday("");
					self::setStepall(null);
					self::setAchievementday("");
					self::setAchievementall("");
					self::setStepTopPre(null);
					self::setStepTop(null);
					self::setCreatedate("");
					self::setUpdatedate("");

				}
			}
				
			
		   				
			
					
				
			public function setId($value) {
			       $this->array = array_merge ($this->array,array(
			       'ID' => $value));
		     	} 
			public function getId(){							
			      return $this->array ['ID'];
	            }
			public function setOpenid($value) {
				$this->array = array_merge ($this->array,array(
					'OPENID' => $value));
			}
			public function getOpenid(){
				return $this->array ['OPENID'];
			}

			public function setImg($value) {
				$this->array = array_merge ($this->array,array(
					'WXIMG' => $value));
			}
			public function getImg(){
				return $this->array ['WXIMG'];
			}

			public function setName($value) {
			       $this->array = array_merge ($this->array,array(
			       'NAME' => $value));
			} 
			public function getName(){							
			      return $this->array ['NAME'];
			}
			public function setMarkName($value) {
				$this->array = array_merge ($this->array,array(
					'MARKNAME' => $value));
			}
			public function getMarkName(){
				return $this->array ['MARKNAME'];
			}

			public function setState($value) {
				$this->array = array_merge ($this->array,array(
					'STATE' => $value));
			}
			public function getState(){
				return $this->array ['STATE'];
			}

			public function setPositionDay($value) {
				$this->array = array_merge ($this->array,array(
					'POSITIONDAY' => $value));
			}
			public function getPositionDay(){
				return $this->array ['POSITIONDAY'];
			}

			public function setPositionAll($value) {
				$this->array = array_merge ($this->array,array(
					'POSITIONALL' => $value));
			}
			public function getPositionAll(){
				return $this->array ['POSITIONALL'];
			}


			public function setStepyestoday($value) {
				$this->array = array_merge ($this->array,array(
					'STEPYESTODAY' => $value));
			}
			public function getStepyestoday(){
				return $this->array ['STEPYESTODAY'];
			}

			public function setStepday($value) {
			       $this->array = array_merge ($this->array,array(
			       'STEPDAY' => $value));
			} 
			public function getStepday(){
			      return $this->array ['STEPDAY'];
			} 
					
				
			public function setStepall($value) {
			       $this->array = array_merge ($this->array,array(
			       'STEPALL' => $value));
			} 
			public function getStepall(){
			      return $this->array ['STEPALL'];
			}

			public function setStepTopPre($value) {
				$this->array = array_merge ($this->array,array(
					'STEPTOPPRE' => $value));
			}
			public function getStepTopPre()
			{
				return $this->array ['STEPTOPPRE'];
			}

			public function setStepTop($value) {
				$this->array = array_merge ($this->array,array(
					'STEPTOP' => $value));
			}
			public function getStepTop(){
				return $this->array ['STEPTOP'];
			}

			public function setAchievementday($value) {
				$this->array = array_merge ($this->array,array(
					'ACHIEVEMENTDAY' => $value));
			}
			public function getAchievementday(){
				return $this->array ['ACHIEVEMENTDAY'];
			}


			public function setAchievementall($value) {
				$this->array = array_merge ($this->array,array(
					'ACHIEVEMENTALL' => $value));
			}
			public function getAchievementall(){
				return $this->array ['ACHIEVEMENTALL'];
			}




			public function setCreatedate($value) {
			       $this->array = array_merge ($this->array,array(
			       'CREATEDATE' => $value));
			} 
			public function getCreatedate(){							
			      return $this->array ['CREATEDATE'];
			} 
					
				
			public function setUpdatedate($value) {
			       $this->array = array_merge ($this->array,array(
			       'UPDATEDATE' => $value));
			} 
			public function getUpdatedate(){							
			      return $this->array ['UPDATEDATE'];
			} 
		
			
			public function toInsertCSV() {
						 $str="";
				
						// id
						$str .= "'". self::getId()."',";
						//
						$str .= "'". self::getOpenid()."',";
						//Img
						$str .= "'". self::getImg()."',";
						// name
						$str .= "'". self::getName()."',";

						// markName
						$str .= "'". self::getMarkName()."',";
						// state
						$str .= "'". self::getState() . "',";
						//PositionDay
						$str .= "'". self::getPositionDay()."',";
						//PositionAll
						$str .= "'". self::getPositionAll()."',";
						// stepyestoday
						$str .= "'". self::getStepyestoday()."',";

						// stepday
						$str .= "'". self::getStepday()."',";

						// stepall
						$str .= "'". self::getStepall()."',";
						//Achievementday
						$str .= "'". self::getAchievementday()."',";

						//Achievementall
						$str .= "'". self::getAchievementall()."',";

						//StepTopPre
						$str .= "'". self::getStepTopPre()."',";

						//StepTop
						$str .= "'". self::getStepTop()."',";

						//createdate
						$str .= 'now()' . ',';

						// updatedate
						$str .= 'now()';
						return $str;
			}
				
		
			
			public function toUpdateSet() {
						 $str="";
				
						// name
						$str.= 'NAME = "' . self::getName() . '",';
						//Img
						$str.= 'WXIMG = "' . self::getImg() . '",';
						// markName
						$str.= 'MARKNAME = "' . self::getMarkName() . '",';
						// state
						$str.= 'STATE = "' . self::getState() . '",';
						
						// stepday
						$str.= 'STEPDAY = "' . self::getStepday() . '",';
						
						// stepall
						$str.= 'STEPALL = "' . self::getStepall() . '",';

						// stepyestoday
						$str.= 'STEPYESTODAY = "' . self::getStepyestoday() . '",';

						//positionday
						$str.= 'POSITIONDAY = "' . self::getPositionDay() . '",';
						//positionall
						$str.= 'POSITIONALL = "' . self::getPositionAll() . '",';


						//Achievementday
						$str.= 'ACHIEVEMENTDAY = "' . self::getAchievementday() . '",';

						//Achievementall
						$str.= 'ACHIEVEMENTALL = "' . self::getAchievementall() . '",';
						//StepTopPre
						$str.= 'STEPTOPPRE = "' . self::getStepTopPre() . '",';

						//StepTop
						$str.= 'STEPTOP = "' . self::getStepTop() . '",';

						

						// updatedate
						$str .= "UPDATEDATE = now()";
						return $str;
			}
			public function toArray() {
				$strcture = array (
					"ID" => self::getId(),
					"OPENID" => self::getOpenid(),
					"WXIMG" => self::getImg(),
					"NAME" => self::getName(),
					//"STATE" => self::getState(),
					"ACHIEVEMENTALL" => self::getAchievementall(),
					"STEPTOP" => self::getStepTop(),
					//"POSITIONDAY" => self::getPositionDay(),
					"POSITIONALL" => self::getPositionAll(),
					//"UPDATEDATE" => self::getCreatedate(),
					//"UPDATEDATE" => self::getUpdatedate()
				);
				return $strcture;
			}
		
			public function listToArray() {
				$strcture = array (
					"OPENID" => self::getOpenid(),
					"NAME" => self::getName(),
					'WXIMG' => self::getImg(),
					"POSITIONDAY" => self::getPositionDay(),
					'STEPYESTODAY' => self::getStepyestoday(),
					"ACHIEVEMENTALL" => self::getAchievementall()
					//"STEPTOP" => self::getStepTop()
				);
				return $strcture;
			}
			
			public function toUpdateWhere(){
						 $str = ""; 
						$str .= "OPENID = '" . self::getOpenid()."'";
						 return $str;
			}
		
}		
									
  	
class Step_count_DAO {
									

				public static $TABLENAME = 'STEP_COUNT';

								
			
			
				public static function getRecord($openid,$dbCon){
						 $where = "OPENID = '$openid'";
						 $record = SearchDb::getRecord($dbCon,static::$TABLENAME,$where);
						 if($record != null) {
							 return new Step_count($record);
						 }
						  
						 return null;
			    }
		
			
			
				public static function getList($dbCon) {
						 $where = "";
						 $list = SearchDb::getList($dbCon,static::$TABLENAME,$where);
						 $results = array();
						 
						foreach($list as $record){
						 	array_push ( $results, new Step_count($record));
						}
						 return $results;
				}

				public static function getListByNeedle($needle,$dbCon) {


					$where = "NAME like '%$needle%' OR MARKNAME like '%$needle%' ";
					$list = SearchDb::getList($dbCon,static::$TABLENAME,$where);
					$results = array();

					foreach($list as $record){
						array_push ( $results, new Step_count($record));
					}
					return $results;
				}



				public static function getRecordMem($id,$mstCon,$memCon) {
									 
									$listAll = self::getListMem ( $mstCon,$memCon);
									 
									foreach($listAll as $record) {
									
										if($record->getId() == $id){
			
											return $record;
			
										}
									}
			
									return null;
			    }

			
			
			   public static function getListMem($mstCon,$memCon){
						 
						$listAll = SearchDb::getListMem($mstCon,$memCon,static::$TABLENAME);
						 
						$results = array();
						 
						foreach($listAll as $record){
						 array_push ( $results,new Step_count($record));
								 
			     		}
			   return $results;
			   }





			//按昨日步数排名
			
				public static function getListByOrderDayLimit($dbCon) {
					$where = "1=1 ORDER BY STEPYESTODAY DESC LIMIT 0,10";
					$list = SearchDb::getList($dbCon,static::$TABLENAME,$where);
					$results = array();
			
					foreach($list as $record){
						array_push ( $results, new Step_count($record));
					}
					return $results;
				}
			////按昨日步数排名
				public static function getListByOrderDay($dbCon) {
					$where = "1=1 ORDER BY STEPYESTODAY DESC";
					$list = SearchDb::getList($dbCon,static::$TABLENAME,$where);
					$results = array();
			
					foreach($list as $record){
						array_push ( $results, new Step_count($record));
					}
					return $results;
				}
			
				//按总积分排名
				public static function getListByOrderAchieve($dbCon)
				{
					$where = "1=1 ORDER BY ACHIEVEMENTALL DESC,STEPALL DESC";
					$list = SearchDb::getList($dbCon, static::$TABLENAME, $where);
					$results = array();

					foreach ($list as $record) {
						array_push($results, new Step_count($record));
					}
					return $results;
				}
				//总积分加检索
				public static function getListByOrderAchieveAndNeedle($needal,$dbCon) {
					$where = "NAME like '%$needal%' OR MARKNAME like '%$needal%' ORDER BY ACHIEVEMENTALL DESC,STEPALL DESC";
					$list = SearchDb::getList($dbCon,static::$TABLENAME,$where);
					$results = array();
			
					foreach($list as $record){
						array_push ( $results, new Step_count($record));
					}
					return $results;
				}
				
				//总积分top10
				public static function getListByOrderAchieveTop($dbCon) {
					$where = "1=1 ORDER BY ACHIEVEMENTALL DESC,STEPALL DESC LIMIT 0,10";
					$list = SearchDb::getList($dbCon,static::$TABLENAME,$where);
					$results = array();
			
					foreach($list as $record){
						array_push ( $results, new Step_count($record));
					}
					return $results;
				}
			
			
				//为用户设置自增id
				public static function getMemCount($dbCon){
					$sql = "select MAX(ID) from " . static::$TABLENAME  ;
					$list = SearchDb::quiryLines($dbCon, $sql);
					$results = array();
					foreach($list as $record){
						$results = $record['MAX(ID)'];
					}
					return $results;
				}
	
	
	
				public static function insert($record,$dbCon){
		
				try {
	
						$openid = $record->getOpenid();
						$oldrecord = self::getRecord($openid,$dbCon);
						if($oldrecord != null) {
						   throw new Exception ("重复插入");
						}
						ChangeDb::insert($dbCon,static::$TABLENAME,$record);
					} catch(Exception $e){
						throw $e;
						}
				}

			
			
			   public static function update($record ,$dbCon){
						try{
							$openid = $record->getOpenid();
								$oldrecord = self::getRecord($openid,$dbCon);
								if($oldrecord == null){
										throw new Exception("不存在该记录");
								}
								ChangeDb::update($dbCon,static::$TABLENAME,$record);
						} catch(Exception $e){
										throw $e;
									}
					 }

				public static function initDayStep($dbCon){
					try{
						$sql = "update ". static::$TABLENAME . " set `STEPDAY` = 0";
						SearchDb::quiryLines($dbCon,$sql);
					} catch(Exception $e){
						throw $e;
					}
				}

				public static function initDayAchieve($dbCon){
					try{
						$sql = "update ". static::$TABLENAME . " set `ACHIEVEMENTDAY` = 0";
						SearchDb::quiryLines($dbCon,$sql);
					} catch(Exception $e){
						throw $e;
					}
				}
				//
				public static function initStepTop($StepTop,$id,$dbCon){


					try{
						$sql = "update ". static::$TABLENAME . " set `STEPTOPPRE` = $StepTop where `ID`=$id";
						echo $sql;
						SearchDb::quiryLines($dbCon,$sql);
					} catch(Exception $e){
						throw $e;
					}
				}
			
			    public static function delete ($record,$dbCon){
						try{
							$id = $record->getId();
							$oldrecord = self::getRecord($id,$dbCon);
							if($oldrecord == null){
									throw new Exception("不存在该记录");
							}
							ChangeDb::delete($dbCon,static::$TABLENAME,$record);
						} catch(Exception $e){
									throw $e;
				        	}
					}
}
										 
?>